package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

// 创建定时任务类
@Component // 不要忘记要往容器中注册
public class ScheduleTasks {
    /*@Scheduled(cron = "* * * * * *") // 表明该方法需要定时的调度执行
    public void testTask1() {
        System.out.println(new Date());
    }*/
    /*
        使用springBoot定时任务步骤:
        1.在引导类上添加@EnableScheduling注解
        2.创建一个类,使用@Component注解,在类中添加方法,使用@Scheduled注解,指定定时规则
        3.编写定时任务业务逻辑
     */


    // 检查租约状态
    // 每天0点检查一次
    // 根据租约结束日期与当前日期比较,如果结束日期小于等于当前日期,则将租约状态设置为已到期

    @Autowired
    private LeaseAgreementService leaseAgreementService;


    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        leaseAgreementLambdaUpdateWrapper
                .lt(LeaseAgreement::getLeaseEndDate, new Date())
                .in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING)
                .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }
}
